<?php

namespace App\Http\Controllers\Admin;

use App\Model\Permission;
use App\Model\Role;
use App\Model\RolePermiss;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class RoleController extends Controller
{

    public function index()
    {
        $role = Role::get();
        return view('admin.role.list',compact('role'));
    }

    public function create()
    {
        return view('admin.role.add');
    }

    public function store(Request $request)
    {
        $input = $request->all();
        $res = Role::create($input);
        if($res){
            echo json_encode([
                "code"=>1,
                "msg"=>"success"
            ]);
        }else{
            echo json_encode([
                "code"=>0,
                "msg"=>"error"
            ]);
        }
    }

    public function edit($id)
    {
        $role = Role::find($id);
        return view('admin.role.edit',compact('role'));
    }

    public function update(Request $request, $id)
    {
        $user = $request -> all();
        $res = Role::where("id",$id)->update($user);
        if($res){
            echo json_encode([
                "code"=>1,
                "msg"=>"success"
            ]);
        }else{
            echo json_encode([
                "code"=>0,
                "msg"=>"error"
            ]);
        }
    }

    public function destroy($id)
    {
        $user = Role::find($id);
        $res = $user->delete();
        if($res){
            $data = array(
                "status"=>0,
                "message"=>"删除成功"
            );
        }else{
            $data = array(
                "status"=>1,
                "message"=>"删除失败"
            );
        }
        return $data;
    }

    public function delAll(Request $request){
        $res = Role::destroy($request->input('ids'));
        if($res){
            $data = array(
                "status"=>0,
                "message"=>"删除成功"
            );
        }else{
            $data = array(
                "status"=>1,
                "message"=>"删除失败"
            );
        }
        return $data;
    }

    public function permiss($id){
        /*不用关联模型需要下面的代码
        $permiss = Permission::get();
        $rolepermiss = RolePermiss::where("role_id",$id)->get();
        foreach ($rolepermiss as $v){
            foreach ($permiss as $key=>$value){
                if($value->id == $v->permission_id){
                    $permiss[$key]['status'] = 1;
                }
            }
        }
        */
        $role = Role::find($id);
        $permiss = Permission::get();
        $own_permission = $role->permission;
        //角色用友的权限的id
        $own_permiss= [];
        foreach ($own_permission as $v){
            $own_permiss[] = $v->id;
        }
        return view("admin.role.rolepermiss",compact('permiss','id','own_permiss'));
    }

    public function modifypermiss(Request $request){
        $permissAll = $request->post('ids');
        $id = $request->post('id');
        if($permissAll){
            RolePermiss::where("role_id",$id)->delete();
            foreach ($permissAll as $v){
                $info['role_id'] = $id;
                $info['permission_id'] = $v;
                RolePermiss::create($info);
            }
        }

    }
}
